package study.sharding.spring.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import study.sharding.spring.support.routing.DataSourceKeyHolder;
import study.sharding.spring.mapper.userbase.UserBaseEO;
import study.sharding.spring.mapper.userbase.UserBaseMapper;
import study.sharding.spring.support.aspect.TargetDataSource;

@Slf4j
@Service("userService")
public class UserService {

    @Autowired
    private UserBaseMapper userBaseMapper;

    public int add(UserBaseEO ubEO) {
        return userBaseMapper.insert(ubEO);
    }

    @TargetDataSource("slav11e")
    public UserBaseEO get(Long ubId) {
        DataSourceKeyHolder.setKey("master");
        UserBaseEO ubEO = userBaseMapper.get(ubId);
        return ubEO;
    }

    @TargetDataSource("slave11")
    public UserBaseEO getLt(Long ubId) {
        UserBaseEO ubEO = userBaseMapper.get(ubId);
        log.info("{}", ubEO);

        DataSourceKeyHolder.setKey("master");
        ubEO = userBaseMapper.get(ubId);
        log.info("{}", ubEO);

        DataSourceKeyHolder.setKey("slave");
        ubEO = userBaseMapper.get(ubId);
        log.info("{}", ubEO);
        return ubEO;
    }
}
